package com.sync.syncmasterdata.customer.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sync.syncmasterdata.customer.pojo.LDTCustomer;
import com.sync.syncmasterdata.customer.service.http.HttpAPIServiceForCrmSync;
import com.sync.syncmasterdata.dingrobot.service.DingRobotService;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * LDT 主数据相关服务
 * 客户，产品
 */
@Service
@Slf4j
public class LDTServiceImpl {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private HttpAPIServiceForCrmSync httpAPIServiceForCrmSync;

    @Autowired
    private DingRobotService dingRobotService;

    @Value("${ldt.url}")
    private String LDTURL;

    /**
     * controller  控制 字符串空
     * 新增 客户档案到LDT系统中
     * @param ldtCustomer
     */
    public JSONObject addCustomerToLDT(LDTCustomer ldtCustomer) {
        String requestUrl = String.format("%s/organization/sync", LDTURL);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        try {
            HttpEntity<String> reqEntity2 = new HttpEntity<>(objectMapper.writeValueAsString(ldtCustomer), headers);

            String res=restTemplate.postForObject(requestUrl,reqEntity2, String.class);
            log.info("打印返回值---->{}", res);
            JSONObject resJson = new JSONObject(res);
            if(resJson.getBoolean("success")){
                return resJson;
            }else{
                log.info("新增不成功");
                dingRobotService.simpleDingSendMsg("推送U8客户信息（更新）出错：LDT新增不成功",new String[]{"15267167276"});
                return null;
            }
        } catch (Exception e) {
            log.info("请求返回异常!!!!");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 修改 ldt 客户 服务
     * @param ldtCustomer
     * @return
     */
    public JSONObject editCustomerToLDT(LDTCustomer ldtCustomer) {
        String requestUrl = String.format("%s/organizaion/sync", LDTURL);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        try {
            HttpEntity<LDTCustomer> reqEntity2 = new HttpEntity<>(ldtCustomer, headers);
            JSONObject resJson=restTemplate.postForObject(requestUrl,reqEntity2, JSONObject.class);
            log.info("打印返回值---->{}", objectMapper.writeValueAsString(resJson));
            if(resJson.getBoolean("success")){
                return resJson;
            }else{
                log.info("LDT修改不成功");
                dingRobotService.simpleDingSendMsg("推送U8客户信息（更新）出错：LDT修改不成功",new String[]{"15267167276"});
                return null;
            }
        } catch (Exception e) {
            log.info("请求返回异常!!!!");
            e.printStackTrace();
            return null;
        }

    }
}
